package com.xteam.youin_supplier.fragment;


import android.nfc.Tag;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.google.gson.JsonSyntaxException;
import com.xteam.youin_supplier.MainActivity;
import com.xteam.youin_supplier.R;
import com.xteam.youin_supplier.adpter.OnlineStationListViewAdapter;
import com.xteam.youin_supplier.config.GlobalData;
import com.xteam.youin_supplier.config.RequestAddr;
import com.xteam.youin_supplier.model.BaseModel;
import com.xteam.youin_supplier.model.OnlineStation;
import com.xteam.youin_supplier.util.http.AjaxCallBack;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;


/**
 * 报表
 */
@EFragment(R.layout.fragment_report)
public class OnlinePointFragment extends BaseFragment {

    @ViewById(R.id.back_btn)
    ImageButton backBtn;
    @ViewById(R.id.funnel)
    ImageView funnel;

    @ViewById(R.id.toptitle_title)
    TextView topTitle;
    @ViewById(R.id.onlint_today_tab)
    ImageView todayTab;
    @ViewById(R.id.onlint_week_tab)
    ImageView weekTab;
    @ViewById(R.id.onlint_month_tab)
    ImageView monthTab;

    @ViewById(R.id.imageView2)
    ImageView totalImg;

    @ViewById(R.id.num)
    TextView num;
    @ViewById(R.id.money)
    TextView money;

    @ViewById(R.id.listview)
    RecyclerView listView;

    private static int layoutheight = 0;


    private String type = "0";//0:本日 1：本周 2：本月

    OnlineStationListViewAdapter onlineStationListViewAdapter;
    OnlinePointFragment onlinePointFragment;

    @AfterViews
    @Override
    void initView() {
        onlinePointFragment = this;
        layoutheight = listView.getLayoutParams().height;
        backBtn.setVisibility(View.GONE);
        topTitle.setText(getResources().getString(R.string.onlinepoint_title));
        funnel.setVisibility(View.VISIBLE);
        todayTab.setSelected(true);

        listView.setLayoutManager(new LinearLayoutManager(getActivity()));
        loadData();
    }

    private void loadData() {
        requestParams = "supplier_id=" + GlobalData.userInfo.getData().getSupplier_id()
                + "&type=" + type;
        requestUrl = RequestAddr.reprotList + requestParams;
        xtHttp.get(requestUrl, new AjaxCallBack<Object>() {
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }

            @Override
            public void onSuccess(Object o) {
                super.onSuccess(o);
                dealData(o);
            }
        });

    }

    OnlineStation onlineStation;

    private void dealData(Object object) {
        try {
            onlineStation = gson.fromJson(object.toString(), OnlineStation.class);
        } catch (JsonSyntaxException e) {
            baseModel = gson.fromJson(object.toString(), BaseModel.class);
            Toast.makeText(getActivity(), baseModel.getMsg(), Toast.LENGTH_SHORT).show();
            return;
        }
        num.setText(onlineStation.getData().getQtys());
        money.setText(onlineStation.getData().getSums());
        if (onlineStation.getData().getQtys() == null) {
            num.setText("0 L");
        } else {
            num.setText(onlineStation.getData().getQtys());
        }
        if (onlineStation.getData().getSums() == null) {
            money.setText("0 元");
        }else{
            money.setText(onlineStation.getData().getSums());
        }
        onlineStationListViewAdapter = new OnlineStationListViewAdapter(onlinePointFragment, onlineStation, type);
        listView.setAdapter(onlineStationListViewAdapter);
        if (onlineStation.getData().getList().size() != 0) {
            listView.setLayoutParams(new RelativeLayout.LayoutParams(listView.getLayoutParams().width, layoutheight * onlineStationListViewAdapter.getItemCount()));
        }
        onlineStationListViewAdapter.notifyDataSetChanged();
    }

    @Click({R.id.onlint_today_tab, R.id.onlint_week_tab, R.id.onlint_month_tab, R.id.funnel})
    @Override
    void Click(View view) {
        switch (view.getId()) {
            case R.id.onlint_today_tab:
                todayTab.setSelected(true);
                weekTab.setSelected(false);
                monthTab.setSelected(false);
                totalImg.setImageDrawable(getActivity().getResources().getDrawable(R.drawable.word_jrzj));
                type = "0";
                loadData();
                break;
            case R.id.onlint_week_tab:
                todayTab.setSelected(false);
                weekTab.setSelected(true);
                monthTab.setSelected(false);
                totalImg.setImageDrawable(getActivity().getResources().getDrawable(R.drawable.word_bzzj));
                type = "1";
                loadData();
                break;
            case R.id.onlint_month_tab:
                todayTab.setSelected(false);
                weekTab.setSelected(false);
                monthTab.setSelected(true);
                totalImg.setImageDrawable(getActivity().getResources().getDrawable(R.drawable.word_byzj));
                type = "2";
                loadData();
                break;
            case R.id.funnel:
                MainActivity.getInstance().switchFragment(this, new TruckTeamSearchListFragment_());
                break;
        }
    }
}
